<?php

/**
 * 商品列表
 */

namespace app\mall\api;

use \app\base\api\BaseApi;

class ListApi extends BaseApi {

    /**
     * 列表
     */
    public function index() {

        $orderUrl = $this->data['order'];
        $pageLimit = $this->data['limit'] ? $this->data['limit'] : 10;


        $sellerId = intval($this->data['seller_id']);
        $classId = intval($this->data['id']);


        $filterWhere = [];
        if($classId) {

            $classIds = target('mall/MallClass')->getSubClassId($classId);
            if ($classIds) {
                //$filterWhere['_sql'] = 'A.class_id in(' . $classIds . ')';
            }
        }

        if($sellerId) {
            $filterWhere['A.seller_id'] = $sellerId;

        }

        $service = target('shop/Filter', 'service');
        $data = $service->getData('mall', $filterWhere);

        $filter = $data['filter'];
        $where = $data['where'];
        $order = $data['order'];

        if($orderUrl == 'pos_id') {
            $order = 'A.pos_id asc' . ','. $order;
            $order = trim($order, ',');
        }

        $model = target('mall/Mall');
        if ($where) {
            $count = $model->countList($where);
            $pageData = $this->pageData($count, $pageLimit);
            $list = $model->loadList($where, $pageData['limit'], $order);
        } else {
            $list = [];
        }

        if ($list) {
            $this->success('ok', [
                'data' => $list,
                'pageData' => [
                    'pageLimit' => $pageLimit,
                    'limit' => count($list),
                    'page' => $pageData['page'],
                    'totalPage' => $pageData['totalPage']
                ]
            ]);
        } else {
            $this->error('暂无更多商品', 404);
        }

    }

}